package gdut.imis.handler;

import com.alibaba.fastjson.JSON;
import gdut.imis.Utils.Result;
import gdut.imis.Utils.WebUtils;
import gdut.imis.enums.Status;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 认证异常处理器
 * @Author JiaFu
 * @Date 2023/4/12 11:54
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        Result<Object> responseResult = new Result<>(Status.FORBIDDEN.getCode(), Status.FORBIDDEN.getMsg());
        String json = JSON.toJSONString(responseResult);
        WebUtils.renderString(response,json);
    }
}
